package site.wenneng.dev.service;

import com.ruoyi.common.core.domain.entity.SysDictData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import site.wenneng.dev.common.enumeration.DicEnum;
import site.wenneng.dev.objs.po.DefineProjectPO;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SimpleBizService {

    private RemoteService remoteService;
    private DictionaryService dictionaryService;

    public void syncProject() {
        List<DefineProjectPO> projectList = remoteService.queryProjectFromRemote(listAllProductIdList());
        Set<String> projectIdList = dictionaryService.listValueSet(DicEnum.CMC_PROJECT);
        for (DefineProjectPO defineProjectPO : projectList) {
            if (projectIdList.contains(defineProjectPO.getId().toString())) {
                continue;
            }
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType(DicEnum.CMC_PROJECT.getValue());
            sysDictData.setDictLabel(defineProjectPO.getName());
            sysDictData.setDictValue(defineProjectPO.getId().toString());
            sysDictData.setDictSort(1L);
            sysDictData.setStatus("0");
            sysDictData.setCreateBy("admin");
            dictionaryService.saveSysDictData(sysDictData);
        }
    }

    public List<Integer> listAllProductIdList() {
        return dictionaryService.listValueSet(DicEnum.CMC_PRODUCT).stream()
                .map(Integer::parseInt).collect(Collectors.toList());
    }

    public List<Integer> listAllProjectIdList() {
        return dictionaryService.listValueSet(DicEnum.CMC_PROJECT).stream()
                .map(Integer::parseInt).collect(Collectors.toList());
    }
}
